//
//  CustomXibView.m
//  Pension
//
//  Created by gavin on 2020/6/17.
//  Copyright © 2020 XLH. All rights reserved.
//

#import "MJUICustomXibView.h"

/**
 * 使用代码创建View时不能使用 nibWithNibName 方式，只能使用 initWithFrame
 * 需要初始化代码时在子类实现 - (void)didLoad
 */
@implementation MJUICustomXibView

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

/// 直接使用纯代码初始化
- (instancetype)initWithFrame:(CGRect)frame {
    self = [super initWithFrame:frame];
    if (self) {
        [self initNibView];
    }
    return self;
}

/// 必须重载并且需要手动加载xib并使用addSubview添加，否则直接在xib中嵌套时不能正常显示，
- (instancetype)initWithCoder:(NSCoder *)decoder {
    self = [super initWithCoder:decoder];
    if (self) {
        [self initNibView];
    }
    return self;
}

- (void)initNibView {
    UIView *view = [[self class] mj_nibInstantiateWithOwner:self];
    //view.frame = self.bounds;
    [self insertSubview:view atIndex:0];
//    [self addSubview:view]; //添加到自己身上（否则显示不出来）
    [view mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.equalTo(self);
    }];
    view.backgroundColor = UIColor.clearColor;
    
    
    // ************  需要初始化代码时在子类实现 - (void)didLoad  ************
    SEL sel = NSSelectorFromString(@"didLoad"); // *** - (void)didLoad
    if ([self respondsToSelector:sel]) {
        //[self performSelector:sel];
        IMP imp = [self methodForSelector:sel];
        void (*func)(id, SEL) = (void *)imp;
        func(self, sel);
    }
    // ************  需要初始化代码时在子类实现 - (void)didLoad  ************
    
}

@end
